home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Programming / C Reference Card 3.0 / C Reference Card / C Reference Card.rsrc / TEXT_410_10.txt < prev    next >
Encoding:
Text File  |  1997-07-17  |  2.4 KB  |  113 lines

  1. EXCEPTIONS : basics, sample usage
  2. _________________________________________________________________________
  3.  
  4.  
  5. BASICS OF EXCEPTION HANDLING
  6.  
  7. C++ provides means for exception (or error) handling.  You use the 'try', 'catch', and 'throw' keywords for this purpose.  When programming, you tell the compiler to 'try' a section of code.  If an error occurs in the section (or in any nested methods/functions in the section), you 'throw' an exception.  The exception is then handled like a hot potato and is passed up the ladder until it is caught.  If an exception is not caught, the program terminates.  Here's the basic syntax:
  8.  
  9.   // exceptions.cp
  10.  
  11.   #include <iostream.h>
  12.  
  13.   void doFunction( float x );
  14.  
  15.   main()
  16.   {
  17.     float val = 5.0;
  18.   
  19.     try
  20.     {  
  21.       doFunction( val );
  22.     }
  23.     catch( const char *s )
  24.     {
  25.       cout << "string exception caught" << endl;
  26.       cout << s << endl;
  27.     }
  28.     catch(...)
  29.     {
  30.       // catch all other exceptions here
  31.     }
  32.  
  33.     return 0;
  34.   }
  35.   
  36.   
  37.    void doFunction( float x )
  38.   {
  39.     float limit = 3.14159;
  40.  
  41.     if( x > limit )
  42.       throw "value exceeds limit";
  43.   }
  44.   // end exceptions.cp
  45.  
  46. In most C++ applications, you 'throw' an exception class object rather than a string or error code.  An example of this is shown below:
  47.  
  48.   // potatoe.cp
  49.  
  50.   #include <iostream.h>
  51.  
  52.   void doFunction( float x );
  53.  
  54.   class HotPotatoe
  55.   {  
  56.     public:
  57.       HotPotatoe(float m, float v) {max=m; val=v;}
  58.       float max;
  59.       float val;
  60.   };
  61.   
  62.  
  63.   main()
  64.   {
  65.     float val = 5.0;
  66.   
  67.     try
  68.     {  
  69.       cout << "Example One:" << endl;
  70.       doFunction( val );
  71.     }
  72.     catch( HotPotatoe &thePotatoe )
  73.     {
  74.       cout << "exception caught" << endl;
  75.       cout << "  max. limit: " << thePotatoe.max << endl;
  76.       cout << "  value: " << thePotatoe.val << endl << endl;
  77.     }
  78.     
  79.     try
  80.     {
  81.       cout << "Example Two:" << endl;
  82.       try
  83.       {
  84.         doFunction( val );
  85.       }
  86.       catch( HotPotatoe )
  87.       {
  88.         cout << "exception caught" << endl;
  89.         cout << "exception not handled" << endl;
  90.         cout << "rethrow exception" << endl;
  91.         throw;
  92.       }
  93.     }
  94.     catch(...)
  95.     {
  96.       cout << "rethrown exception caught" << endl;
  97.     }
  98.  
  99.     return 0;
  100.   }
  101.   
  102.   
  103.   void doFunction( float x )
  104.   {
  105.     float limit = 3.14159;
  106.  
  107.     if( x > limit )
  108.     {
  109.       cout << "throw exception" << endl;
  110.       throw HotPotatoe(limit,x);
  111.     }
  112.   }
  113.   // end potatoe.cp